/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：FunNodeDaoImpl.java
 * History:
 *       2012-1-13: Initially created, tangkf.
 */
package com.xteam.purview.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tangkf.utils.CommonUtils;
import com.xteam.db.IBaseDao;
import com.xteam.purview.entities.PurviewRole;
import com.xteam.purview.entities.PurviewUser;
import com.xteam.purview.entities.PurviewUserRole;
import com.xteam.purview.service.AbsFace;
import com.xteam.purview.service.IPurviewRoleFace;

/**
 * @author tangkf  ---2012-1-13
 */
public class PurviewRoleFace extends AbsFace<PurviewRole> implements IPurviewRoleFace {
	
	private PurviewRoleFace(){}
	
	private static PurviewRoleFace	instance	= new PurviewRoleFace();
	
	public static PurviewRoleFace getInstance(){
		return instance;
	}
	
	/**
	 * 返回用户角色列表
	 * @author tangkf  ---2012-1-18
	 * @param userId
	 * @return
	 */
	public List<PurviewRole> getRoleListByUserId(String userId){
		return null;
	}
	
	
	/**
	 * 返回对某功能节点有权限的角色列表
	 * @author tangkf  ---2012-1-18
	 * @param funNodeId
	 * @return
	 */
	public List<PurviewRole> getRoleListByFunNodeId(String funNodeId){
		return null;
	}

	/**
	 * 返回角色列表中的用户列表
	 * @author wangw 2012-9-25
	 * @param roleId
	 * @return
	 */
	public List<PurviewUser> getUserListByRoleId(String roleId) {
		String hql = IBaseDao.SQL_FROM + PurviewUserRole.REF + IBaseDao.SQL_WHERE + PurviewUserRole.PROP_ROLEID + "=?";
		try {
			Collection<String> userId = new ArrayList<String>();
			List<PurviewUserRole> list = dao.queryAll(hql, new String[] { roleId });
			for (PurviewUserRole pur : list) {
				userId.add(pur.getUserId());
			}
			List<PurviewUser> pus = null;
			if (userId != null && userId.size() > 0) {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("userId", userId);
				String qhql = IBaseDao.SQL_FROM + PurviewUser.REF + IBaseDao.SQL_WHERE + PurviewUser.PROP_USERID + " in (:userId)";
				pus = dao.queryAll(qhql, param);
			}
			return pus;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 保存用户角色关系
	 * @author 2012-9-26
	 * @param roleId 角色ID
	 * @param userId 用户ID
	 * @return
	 */
	public int saveUserRole(String roleId, String[] userId) {
		int r = 0;

		if (CommonUtils.notEmpty(roleId) && userId != null) {
			for (String uid : userId) {
				PurviewUserRole pur = new PurviewUserRole();
				pur.setRoleId(roleId);
				pur.setUserId(uid);
				try {
					String hql = IBaseDao.SQL_FROM + PurviewUserRole.REF + IBaseDao.SQL_WHERE + PurviewUserRole.PROP_ROLEID+ "=?" + IBaseDao.SQL_AND
							+ PurviewUserRole.PROP_USERID + "=?";
					List<?> list = dao.queryAll(hql, new String[] { roleId, uid });
					if (CommonUtils.isEmpty(list)) {
						r = dao.saveObject(pur);
					}
				} catch (Exception e) {
					e.printStackTrace();
					r = 1;
				}
			}
		}
		return r;
	}
	
	/**
	 * 删除角色与用户的关系
	 * @author tangkf  ---2012-9-26
	 * @param roleId
	 * @param userId
	 * @return
	 */
	public int delRoleUser(String roleId, String[] userId) {
		int r = 0;
		String hql = IBaseDao.SQL_DELETE + IBaseDao.SQL_FROM + PurviewUserRole.REF + IBaseDao.SQL_WHERE + PurviewUserRole.PROP_ROLEID + "=:roleId "
				+ IBaseDao.SQL_AND + PurviewUserRole.PROP_USERID + " in(:userId)";
		if (roleId != null && userId != null && userId.length > 0) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("roleId", roleId);
			param.put("userId", userId);
			try {
				r = dao.executeHql(hql, param);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return r;
	}
	
}
